/********************************************************************************
 * Project Name		[JavaEE_Components]
 * File Name     	[MailUtils.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package javamail.cases.utils;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import javamail.cases.bean.User;
import javamail.utils.MailAuthenticator;
import javamail.utils.MailConstants;
import javamail.utils.PasswordUtils;

/**
 * 邮件工具类, 用于发送帐户激活链接和密码重置链接
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class MailUtils {

	private static final String emailSender = "javamail_sns@sina.com";

	/**
	 * 获得Session
	 * 
	 * @return
	 */
	private static Session getSession() {
		Properties properties = new Properties();
		properties.setProperty("mail.transport.protocol", MailConstants.PROTOCOL_SMTP);
		properties.setProperty("mail.smtp.host", MailConstants.SMTP_HOST_OF_SINA);
		properties.setProperty("mail.smtp.port", MailConstants.SMTP_PORT_OF_SINA);
		properties.setProperty("mail.smtp.auth", String.valueOf(MailConstants.IS_AUTHORIZATION_NEED));
		properties.setProperty("mail.debug", String.valueOf(MailConstants.IS_DEBUG_MOD_ENABLED));

		Session session = Session.getInstance(properties,
				new MailAuthenticator(emailSender, PasswordUtils.readPassword(emailSender)));
		return session;
	}

	/**
	 * 发送激活邮件
	 * 
	 * @param user
	 */
	public static void sendAccountActivateMail(User user) {
		Session session = getSession();
		MimeMessage message = new MimeMessage(session);
		try {
			message.setSubject("JavaMail帐户激活");
			message.setSentDate(new Date());
			message.setFrom(new InternetAddress(emailSender));
			message.setRecipient(RecipientType.TO, new InternetAddress(user.getEmail()));
			message.setContent("<a href='" + LinkUtils.genderateActivateLink(user) + "'>点击激活帐户</a>",
					"text/html;charset=UTF-8");
			Transport.send(message);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送重置密码邮件
	 * 
	 * @param user
	 */
	public static void sendAccountResetMail(User user) {
		Session session = getSession();
		MimeMessage message = new MimeMessage(session);
		try {
			message.setSubject("JavaMail重置密码");
			message.setSentDate(new Date());
			message.setFrom(new InternetAddress(emailSender));
			message.setRecipient(RecipientType.TO, new InternetAddress(user.getEmail()));
			message.setContent("<a href='" + LinkUtils.generateResetPasswordLink(user) + "'>点击重置密码</a>",
					"text/html;charset=UTF-8");
			Transport.send(message);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
}
